package com.kk.demo.controller;

import com.kk.demo.service.ExampleService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * the type of spring-boot-prototype
 *
 * <p>
 * .
 *
 * @author 王志强
 * @date 2022-11-14
 */
@RestController
public class ExampleController {

    @Autowired
    private ExampleService exampleService;

    @Autowired
    private BeanFactory beanFactory;

    /** 全局异常测试 */
    @RequestMapping("t")
    public void test(){
        exampleService.test();
        throw new NullPointerException();
    }

    /** 全局异常测试 */
    @RequestMapping("t0")
    public void test0(){
        exampleService.test();
        throw new IllegalArgumentException();
    }

    /** 全局异常测试 */
    @RequestMapping("t1")
    public void test1(){
        ExampleService exampleService = beanFactory.getBean(ExampleService.class);
        exampleService.test();
        int i = 1/0;
    }

    /**
     * 全局日期绑定测试
     *
     * @param id
     * @param date
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/t2", method = RequestMethod.GET)
    public String detail(@RequestParam("id") long id, Date date) {

        System.out.println(date);
        System.out.println(id);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

        String a = null;
        a.equals("a");
        return "ok";
    }



    @GetMapping("/t3")
    private String modelAttributeTest(@ModelAttribute("msg") String msg,
                                      @ModelAttribute("platform") Map<String, String> platform) {

        return "msg：" + msg + "<br>" +
                        "info：" + platform;
    }

}
